﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace CCF20Model
{
    partial class TransferedCustomer : IDataErrorInfo
    {
        CCF20Entities ccf20Ent;
        public TransferedCustomer()
        {
            ccf20Ent = new CCF20Entities();
        }
        partial void OnDeviceKeyChanged()
        {
            this.RemoveError("DeviceKey");
            if (this.DeviceKey != null)
            {
                Device device = ccf20Ent.Devices.Where(d => d.DeviceKey == this.DeviceKey).FirstOrDefault();
                if (device != null && device.CustomerID != this.CustomerID)
                {
                    this.AddError("DeviceKey", "设备序列号重复");
                }
            }
        }
        partial void OnIDCNumChanged()
        {
            this.RemoveError("IDCNum");
            if (this.IDCNum != null)
            {
                Device device = ccf20Ent.Devices.Where(d => d.IDCSimNumber == this.IDCNum).FirstOrDefault();
                if (device != null && device.CustomerID != this.CustomerID)
                {
                    this.AddError("IDCNum", "设备IDC号重复");
                }
            }
        }
        #region IDataErrorInfo Members

        private Dictionary<string, string> m_validationErrors = new Dictionary<string, string>();

        private void AddError(string columnName, string msg)
        {
            if (!m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Add(columnName, msg);
            }

        }

        private void RemoveError(string columnName)
        {
            if (m_validationErrors.ContainsKey(columnName))
            {
                m_validationErrors.Remove(columnName);
            }
        }

        public string Error
        {
            get
            {
                if (m_validationErrors.Count > 0)
                {
                    return "Customer data is invalid";
                }
                else return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                if (m_validationErrors.ContainsKey(columnName))
                {
                    return m_validationErrors[columnName];
                }
                else return null;
            }
        }

        #endregion
    }
}
